/**
 * @author yexiebao
 * @date 2025/5/15
 */
public class MyLinkedList {
    private class ListNode {
        int val;
        ListNode next;
        public ListNode() {}
        public ListNode(int val) {
            this.val = val;
        }
        public ListNode(int val, ListNode next){
            this.val = val;
            this.next = next;
        }
    }


    private ListNode head;
    private int size;

    public MyLinkedList() {
        head = null;
        size = 0;
    }

    //  获取第index个节点的数值
    public int get(int index) {
        if(index < 0||index >= size){
            return -1;
        }
        ListNode curr = head;
        if(curr == null){
            return -1;
        }
        while(index>0 && curr.next != null){
            curr = curr.next;
            index--;
        }
        return curr.val;
    }

    // 在链表最前面插入一个节点
    public void addAtHead(int val) {
        ListNode newNode = new ListNode(val);
        newNode.next = head;
        head = newNode;
        size++;
    }

    // 在链表最后插入一个节点
    public void addAtTail(int val) {
        ListNode newNode = new ListNode(val);
        ListNode curr = head;
        while(curr != null && curr.next != null){
            curr = curr.next;
        }
        if(curr == null){
            head = newNode;
        }else{
            curr.next = newNode;
        }
        size++;
    }

    // 在链表中插入一个节点
    public void addAtIndex(int index, int val) {
        if(index <= 0){
           addAtHead(val);
        } else if(index == size){
            addAtTail(val);
        } else if(index > size){
            return;
        }else {
            ListNode newNode = new ListNode(val);
            ListNode curr = head;
            for(int i = 0; i < index - 1; i++){
                curr = curr.next;
            }
            newNode.next = curr.next;
            curr.next = newNode;
            size++;
        }

    }

    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size){
            return;
        }
        ListNode curr = head;

        //处理第一个节点
        if(index == 0){
            head = head.next;
        }else{
            for(int i = 0;i <index - 1;i++){
                curr = curr.next;
            }
            curr.next = curr.next.next;
        }
        size--;
    }
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();

        list.addAtHead(5);                  // [5]
        list.addAtIndex(1, 2);              // [5, 2]
        System.out.println(list.get(1));    // 输出：2

        list.addAtHead(6);                  // [6, 5, 2]
        list.addAtTail(2);                  // [6, 5, 2, 2]
        System.out.println(list.get(3));    // 输出：2

        list.addAtTail(1);                  // [6, 5, 2, 2, 1]
        System.out.println(list.get(5));    // 越界，输出：-1

        list.addAtHead(2);                  // [2, 6, 5, 2, 2, 1]
        System.out.println(list.get(2));    // 输出：5

        list.addAtHead(6);                  // [6, 2, 6, 5, 2, 2, 1]

    }
}

